package com.cxs.plan.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cxs.plan.DTO.RoleDTO;
import com.cxs.plan.annotation.PagingQuery;
import com.cxs.plan.VO.RoleVO;
import com.cxs.plan.domain.entity.Role;
import com.cxs.plan.mapper.RoleWithMenuMapper;
import com.cxs.plan.service.RoleService;
import com.cxs.plan.mapper.RoleMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author Administrator
* @description 针对表【role(角色列表)】的数据库操作Service实现
* @createDate 2024-03-26 09:55:00
*/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
    implements RoleService{

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleWithMenuMapper roleWithMenuMapper;

    @Override
    @PagingQuery
    public List<Role> selectRoleList(String name) {
        return roleMapper.selectRoleList(name);
    }

    @Override
    @Transactional
    public void saveRole(RoleDTO roleDTO) {
        Role role = new Role();
        BeanUtils.copyProperties(roleDTO, role);
        roleMapper.saveRole(role);
        roleWithMenuMapper.saveRoleWithMenu(role.getId(), roleDTO.getMenuIds());
    }

    @Override
    public RoleVO selectRoleById(Integer id) {
        Role role = roleMapper.selectById(id);
        RoleVO roleVO = new RoleVO();
        BeanUtils.copyProperties(role, roleVO);
        roleVO.setMenuIds(roleWithMenuMapper.selectMenuIdsByRoleId(id));
        return roleVO;
    }

    @Override
    public void updateRole(RoleDTO role) {
        Role roleEntity = new Role();
        BeanUtils.copyProperties(role, roleEntity);
        roleMapper.updateById(roleEntity);
        roleWithMenuMapper.deleteRoleWithMenuByRoleId(role.getId());
        roleWithMenuMapper.saveRoleWithMenu(role.getId(), role.getMenuIds());
    }

    @Transactional
    @Override
    public void deleteRole(Integer id) {
        roleMapper.deleteById(id);
        roleWithMenuMapper.deleteRoleWithMenuByRoleId(id);
    }

    @Override
    public List<Role> selectAllRole() {
        return roleMapper.selectList(null);
    }
}




